package com.atguigu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Date:2021/3/9
 * Author:ybc
 * Description:
 */
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、设置编码
        request.setCharacterEncoding("UTF-8");
        //2、获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //3、处理登录的请求
        if("admin".equals(username) && "123456".equals(password)){
            //表示登录成功，重定向到成功页面
            response.sendRedirect("success.html");
        }else{
            //表示登录失败，转发到原页面即login.html
            request.getRequestDispatcher("/login.html").forward(request, response);
        }
    }
}
